﻿using System;
using System.Xml;
using LeoShi.Soft.OpenSinaAPI;
using System.IO;
using System.Data;
using WeiBo.SinaAPI;
namespace TCS.Web.MembersHome
{
    public partial class reg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["reg"] != null)
            {
                if (Request.QueryString["reg"] == "1")
                {
                    //this.dvBindAccount.Visible = false;
                    this.dvReg.Visible = true;
                }
            }
        }
        protected void sinaButton_Click(object sender, EventArgs e)
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET) as HttpGet;
            httpRequest.GetRequestToken();
            string url = httpRequest.GetAuthorizationUrl();
            Session["oauth_token"] = httpRequest.Token;
            Session["oauth_token_secret"] = httpRequest.TokenSecret;
            Response.Redirect(url + "&oauth_callback=http://localhost:4040/membersHome/reg.aspx?reg=1");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string userName = this.TextBox1.Text ;
            string userEmail = this.TextBox3.Text;
            string userPwd = this.TextBox2.Text;
            string userLinker = this.TextBox5.Text;
            string userPhone = this.TextBox6.Text;


            TCS.Model.MemberInfo model = new TCS.Model.MemberInfo();
            model.MI_username = userName;
            model.MI_Email = userEmail;
            model.MI_password = userPwd;
            model.MI_phone = userPhone;
            model.MI_TrueName = userLinker;

            int userid = new TCS.BLL.MemberInfo().Add(model);

            this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('注册成功！');location.href='BindWb.aspx?u="+userid.ToString()+"';</script>");

            #region 获得新浪账户信息

            //var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            //httpRequest.Token = Session["oauth_token"].ToString();
            //httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            //httpRequest.UserRemoteIP = Request.UserHostAddress;
            //string url = "http://api.t.sina.com.cn/users/show.xml?";

            //DataSet xds = new DataSet();
            ////FileStream fsReadXml = new FileStream(url, FileMode.OpenOrCreate, FileAccess.Read);

            ////XmlTextReader myXmlReader = new XmlTextReader(fsReadXml);

            ////xds.ReadXml(myXmlReader);

            ////myXmlReader.Close();
            //string xmlresult = httpRequest.Request(url, "user_id=");
            //xds.ReadXml(xmlresult);
            #endregion


            //TCS.Model.Weibo.WeiBo_UserInfo model = new TCS.Model.Weibo.WeiBo_UserInfo();
            //model.WB_AccountName = userName;
            //model.WB_Pwd = userPwd;


            //string oauth_token = Session["oauth_token"].ToString();
            //string oauth_verifier = Request["oauth_verifier"];

            //if (!string.IsNullOrEmpty(oauth_token) && !string.IsNullOrEmpty(oauth_verifier) && SinaBase.oAuth != null)
            //{
            //    oAuthSina oauth = SinaBase.oAuth;
            //    oauth.Verifier = oauth_verifier;
            //    oauth.Token = oauth_token;
            //    oauth.AccessTokenGet(oauth_token);

            //    SinaApiWebService sina = new SinaApiWebService();
            //    SinaBase.User = UserController.GetModelByUserXml(sina.users_show(oauth, "xml", "user_id=" + oauth.AccessUesrID));
            //}
            //string wb_id = "";
            //string WB_AccountName = "";
            //string WB_Pwd = "";
            //int Member_ID = "";
            //string oauth_token = Session["oauth_token"].ToString();
            //string oauth_token_secret = Session["oauth_token_secret"].ToString();
            //if (WeiBo.SinaAPI.SinaBase.User != null)
            //{ 
            //    WeiBo.SinaAPI.SinaMUsers user = WeiBo.SinaAPI.SinaBase.User;
            //    wb_id = user.id.ToString();
            //    WB_AccountName = user.name;
            //}

            
        }
    }
}
